package com.liuheng.manager.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liuheng.manager.mapper.OrderStatisticsMapper;
import com.liuheng.manager.service.OrderInfoService;
import com.liuheng.spzx.model.dto.order.OrderStatisticsDto;
import com.liuheng.spzx.model.entity.order.OrderStatistics;
import com.liuheng.spzx.model.vo.order.OrderStatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;
    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {
        String createTimeBegin = orderStatisticsDto.getCreateTimeBegin();
        String createTimeEnd = orderStatisticsDto.getCreateTimeEnd();
        LambdaQueryWrapper<OrderStatistics> wrapper = new LambdaQueryWrapper<>();
        wrapper.le(orderStatisticsDto!=null&&orderStatisticsDto.getCreateTimeBegin()!="",OrderStatistics::getOrderDate,orderStatisticsDto.getCreateTimeEnd())
                        .ge(orderStatisticsDto!=null&&orderStatisticsDto.getCreateTimeBegin()!="",OrderStatistics::getOrderDate,orderStatisticsDto.getCreateTimeBegin());
        List<OrderStatistics> orderStatisticsList = orderStatisticsMapper.selectList(wrapper);
        List<String> dateList = new ArrayList<>();
        ArrayList<BigDecimal> amountList = new ArrayList<>();
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        orderStatisticsVo.setAmountList(amountList);
        orderStatisticsVo.setDateList(dateList);
        for (OrderStatistics orderStatistics : orderStatisticsList) {
            orderStatisticsVo.getDateList().add(DateUtil.format(orderStatistics.getOrderDate(),"yyyy-MM-dd"));
            orderStatisticsVo.getAmountList().add(orderStatistics.getTotalAmount());
        }
        return orderStatisticsVo;
    }
}
